package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-03-01 22:27
 **/
public class LeetCode2187 {

    public long minimumTime(int[] time, int totalTrips) {
        long l = 1L;
        long min = Long.MAX_VALUE;
        for (int j : time) {
            min = Math.min(min, j);
        }
        long r = min * totalTrips;
        while (l < r) {
            long m = l + r >> 1;
            long sum = getSum(time, m);
            if (sum >= totalTrips) {
                r = m;
            } else {
                l = m + 1;
            }
        }
        return l;
    }

    long getSum(int[] time, long m) {
        long sum = 0;
        for (int i : time) {
            sum += m / i;
        }
        return sum;
    }

}
